package com.coda.app.service.impl;

import com.coda.app.mapper.*;
import com.coda.app.service.BrowsingHistoryService;
import com.coda.app.service.PaymentTypeService;
import com.coda.app.service.RoomService;
import com.coda.appVo.apartment.ApartmentItemVo;
import com.coda.appVo.attr.AttrValueVo;
import com.coda.appVo.fee.FeeValueVo;
import com.coda.appVo.graph.GraphVo;
import com.coda.appVo.room.RoomDetailVo;
import com.coda.appVo.room.RoomItemVo;
import com.coda.appVo.room.RoomQueryVo;
import com.coda.context.BaseContext;
import com.coda.entity.*;
import com.coda.result.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private GraphMapper graphMapper;
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private AttrMapper attrMapper;
    @Autowired
    private ApartmentMapper apartmentMapper;
    @Autowired
    private FacilityMapper facilityMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeMapper feeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    /**
     * 分页查询房间列表
     *
     * @param current
     * @param size
     * @param roomQueryVo
     * @return
     */
    @Override
    public PageResult getPage(Integer current, Integer size, RoomQueryVo roomQueryVo) {
        //设置分页参数
        PageHelper.startPage(current,size);
        //分页查询
        Page<RoomItemVo> roomItemVoPage =  roomMapper.getPage(roomQueryVo);

        //遍历查询出来的房间信息
        for (RoomItemVo roomItemVo : roomItemVoPage) {
            //获取每个房间对应的图片，并赋值给roomItemVo
            List<GraphVo> graphVoList = graphMapper.getGraph(roomItemVo.getId(),2);
            roomItemVo.setGraphVoList(graphVoList);

            //获取每个房间对应的标签赋值给roomItemVo
            List<LabelInfo> labelInfoList = labelMapper.getLabel(roomItemVo.getId());
            roomItemVo.setLabelInfoList(labelInfoList);

        }

        PageResult pageResult = new PageResult(roomItemVoPage.getTotal(), roomItemVoPage.getResult());
        return pageResult;
    }

    /**
     * 根据id查询房间详细信息
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //获取房间信息
        RoomInfo roomInfo = roomMapper.getRoomInfo(id);

        //获取公寓信息
        ApartmentInfo apartmentInfo = apartmentMapper.getApartment(id);
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        //获取公寓标签
        List<LabelInfo> aprLabelInfoList = labelMapper.getApartmentLabel(apartmentInfo.getId());
        //获取公寓图片
        List<GraphVo> aprGraphVoList = graphMapper.getGraph(apartmentInfo.getId(), 1);
        //查询公寓里面房间最小金额
        BigDecimal money = roomMapper.getMin(apartmentInfo.getId());
        apartmentItemVo.setGraphVoList(aprGraphVoList);
        apartmentItemVo.setLabelInfoList(aprLabelInfoList);
        apartmentItemVo.setMinRent(money);

        //获取房间图片信息列表
        List<GraphVo> roomGraphVoList = graphMapper.getGraph(id, 2);
        //获取房间属性列表
        List<AttrValueVo> attrValueVoList = attrMapper.getAttr(id);
        //获取房间配套信息
        List<FacilityInfo> facilityInfoList = facilityMapper.getRoomFacility(id);
        //获取房间标签
        List<LabelInfo> roomLabelInfoList = labelMapper.getLabel(id);
        //获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.getRoomPaymentList(id);
        //获取杂费信息
        List<FeeValueVo> feeValueVos = feeMapper.getFee(apartmentInfo.getId());
        //获取租期信息列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTerm(apartmentInfo.getId(),id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(roomGraphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(roomLabelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVos);
        roomDetailVo.setLeaseTermList(leaseTermList);

        //保存历史浏览记录
        browsingHistoryService.saveHistory(BaseContext.getCurrentId(),id);
        return roomDetailVo;
    }

    /**
     * 根据公寓id分页查询房间信息
     * @param current
     * @param size
     * @param id
     * @return
     */
    @Override
    public PageResult getPageByApratId(Integer current, Integer size, Long id) {
        //设置分页参数
        PageHelper.startPage(current,size);
        Page<RoomItemVo> roomItemVoPage = roomMapper.getPageByApratId(id);
        for (RoomItemVo roomItemVo : roomItemVoPage) {
            //获取房间图片
            List<GraphVo> graphVoList = graphMapper.getGraph(roomItemVo.getId(), 2);
            //获取房间标签信息
            List<LabelInfo> labelInfoList = labelMapper.getLabel(roomItemVo.getId());
            ApartmentInfo apartment = apartmentMapper.getApartment(roomItemVo.getId());
            roomItemVo.setGraphVoList(graphVoList);
            roomItemVo.setLabelInfoList(labelInfoList);
            roomItemVo.setApartmentInfo(apartment);
        }
        PageResult pageResult = new PageResult(roomItemVoPage.getTotal(), roomItemVoPage.getResult());
        return pageResult;
    }
}
